package com.wuyan.web.base.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wuyan.web.base.aop.ApiLogAnnotation;
import com.wuyan.web.base.entity.PubFunction;
import com.wuyan.web.base.helper.BaseApi;
import com.wuyan.web.base.helper.rep.RepBody;
import com.wuyan.web.base.helper.rep.RepHelper;
import com.wuyan.web.base.service.PubFunctionServiceExtend;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 扩展接口
 */

@Slf4j
@RestController
@RequestMapping("/api/pub-function")
public class PubFunctionApiExtend extends BaseApi implements RepHelper {
    @Autowired
    private ObjectMapper mapper;

    @Autowired
    private PubFunctionServiceExtend pubFunctionServiceExtend;

    /**
     * 菜单导入-JSON
     *
     * @param jsonStr 完整的菜单配置信息，JSON格式
     * @return RepBody<List < PubFunction>>
     */
    @PostMapping("/import-json")
    @ApiLogAnnotation(name = "PubFunction:import-json")
    @Transactional(rollbackFor = Exception.class)
    public RepBody<List<PubFunction>> importJSON(@RequestBody String jsonStr) throws JsonProcessingException {
        jsonStr = jsonStr.replace("\n", "");
        jsonStr = jsonStr.replace("\t", "");
        JsonNode jsonNode = mapper.readTree(jsonStr);

        return ok(pubFunctionServiceExtend.importJSON(jsonNode));
    }
}